<?php

// ==============================================================================
//
// This file is part of the WelSpeak.
//
// Create by Welfony Support <support@welfony.com>
// Copyright (c) 2012-2014 welfony.com
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// ==============================================================================

namespace Welfony\Welspeak\Repository;

class MessageRepository
{

    public static function findMessageById($id)
    {
        global $app;

        $strSql = 'SELECT
                       *
                   FROM Message M
                   WHERE M.MessageId = ?
                   LIMIT 1';

        return $app->conn->fetchAssoc($strSql, array($id));
    }

    public static function getAllMessagesCountByReceiverAndSender($receiverId, $senderId)
    {
        global $app;

        $strSql = "SELECT
                       COUNT(1) `Total`
                   FROM Message M
                   WHERE (M.SenderId = ? AND M.ReceiverId = ?) OR (M.ReceiverId = ? AND M.SenderId = ?)
                   LIMIT 1";

        $row = $app->conn->fetchAssoc($strSql, array($senderId, $receiverId, $senderId, $receiverId));

        return $row['Total'];
    }

    public static function getAllMessagesByReceiverAndSender($receiverId, $senderId, $page, $pageSize)
    {
        global $app;

        $offset = ($page - 1) * $pageSize;

        $strSql = "SELECT
                       M.MessageId,
                       M.SenderId,
                       M.ReceiverId,
                       M.MediaUrl,
                       M.MediaLength,
                       M.CreatedDate
                   FROM Message M
                   WHERE (M.SenderId = ? AND M.ReceiverId = ?) OR (M.ReceiverId = ? AND M.SenderId = ?)
                   ORDER BY M.MessageId ASC
                   LIMIT $offset, $pageSize";

        return $app->conn->fetchAll($strSql, array($senderId, $receiverId, $senderId, $receiverId));
    }

    public static function updateStatusByReceiver($receiverId, $status)
    {
        global $app;

        try {
            return $app->conn->update('Message', array('Status' => $status), array('ReceiverId' => $receiverId));
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }
    }

    public static function save($data)
    {
        global $app;

        try {
            if ($app->conn->insert('Message', $data)) {
                return $app->conn->lastInsertId();
            }
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }

        return false;
    }

    public static function update($messageId, $data)
    {
        global $app;

        try {
            return $app->conn->update('Message', $data, array('MessageId' => $messageId));
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }
    }

    public static function remove($messageId)
    {
        global $app;

        try {
            $app->conn->delete('Message', array('MessageId' => $messageId));
            return true;
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }
    }

}